package com.yougou.ui.product;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.yougou.base.BaseViewModel;
import com.yougou.data.model.Goods;
import com.yougou.data.model.GoodsDetail;
import com.yougou.data.repository.ProductRepository;
import com.yougou.utils.CartManager;
import com.yougou.utils.UserManager;

import java.util.ArrayList;
import java.util.Arrays;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class ProductDetailViewModel extends BaseViewModel {
    
    private final ProductRepository mRepository;
    private MutableLiveData<GoodsDetail> goodsDetail = new MutableLiveData<>();
    private MutableLiveData<String> message = new MutableLiveData<>();
    private MutableLiveData<Boolean> loading = new MutableLiveData<>(false);
    private MutableLiveData<Boolean> needLogin = new MutableLiveData<>();
    private MutableLiveData<GoodsDetail> buyNowData = new MutableLiveData<>();
    
    public ProductDetailViewModel() {
        mRepository = new ProductRepository();
    }
    
    public LiveData<GoodsDetail> getGoodsDetail() {
        return goodsDetail;
    }
    
    public LiveData<String> getMessage() {
        return message;
    }
    
    public LiveData<Boolean> isLoading() {
        return loading;
    }
    
    public LiveData<Boolean> getNeedLogin() {
        return needLogin;
    }
    
    public LiveData<GoodsDetail> getBuyNowData() {
        return buyNowData;
    }
    
    
    public void loadGoodsDetail(int goodsId) {
        if (goodsId <= 0) {
            message.setValue("商品ID无效");
            return;
        }
        
        loading.setValue(true);
        addDisposable(
            mRepository.getGoodsDetail(goodsId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                    response -> {
                        loading.setValue(false);
                        if (response.isSuccess() && response.getMessage() != null) {
                            goodsDetail.setValue(response.getMessage());
                        } else {
                            message.setValue(response.getMeta() != null ? response.getMeta().getMsg() : "获取商品详情失败");
                        }
                    },
                    error -> {
                        loading.setValue(false);
                        message.setValue("网络错误: " + error.getMessage());
                    }
                )
        );
    }
    
    
    public void addToCart(GoodsDetail detail) {
        // 检查登录状态
        if (!UserManager.isLogin()) {
            needLogin.setValue(true);
            message.setValue("请先登录");
            return;
        }
        
        if (detail == null) {
            message.setValue("商品信息获取失败");
            return;
        }
        
        Goods goods = detail.toGoods();
        CartManager.addToCart(goods);
        message.setValue("已添加到购物车");
    }
    
    
    public void buyNow(GoodsDetail detail) {
        // 检查登录状态
        if (!UserManager.isLogin()) {
            needLogin.setValue(true);
            message.setValue("请先登录");
            return;
        }
        
        if (detail == null) {
            message.setValue("商品信息获取失败");
            return;
        }
        
        // 触发立即购买，传递商品详情
        buyNowData.setValue(detail);
    }
}

